<template>
  <el-drawer v-model="dialogVisible" title="钱包余额明细" direction="rtl" size="60%">
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="钱包编号" align="center" prop="walletId" min-width="80" /> -->
      <el-table-column label="业务标题" align="center" prop="title" width="100" />
      <el-table-column label="交易金额" align="center" prop="price" min-width="120">
        <template #default="{ row }">
          <div v-if="row.price >= 0" class="text-orange-500">+{{ fenToYuan(row.price) }} 元</div>
          <div v-else class="text-green-500">{{ fenToYuan(row.price) }} 元</div>
        </template>
      </el-table-column>
      <el-table-column label="钱包余额" align="center" prop="balance" min-width="150">
        <template #default="{ row }">
          <span class="text-red-500">{{ fenToYuan(row.balance) }} 元</span>
        </template>
      </el-table-column>
      <el-table-column
        :formatter="dateFormatter"
        align="center"
        label="交易时间"
        prop="createTime"
        width="180px"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
    <template #footer>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import * as WalletTransactionApi from '@/api/pay/wallet/transaction'
import * as WalletApi from '@/api/pay/wallet/balance'
import { fenToYuan } from '@/utils'

defineOptions({ name: 'WalletTransactionList' })

const dialogVisible = ref(false) // 弹窗的是否展示
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  walletId: undefined
})
const list = ref<any>([]) // 列表的数据
/** 查询列表 */
const getList = async () => {
  try {
    loading.value = true
    const data = await WalletTransactionApi.getWalletTransactionPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const open = async (walletId: number | string, userId: number | string = '') => {
  dialogVisible.value = true
  try {
    loading.value = true
    if (userId) {
      const wallet = await WalletApi.getWallet({ userId: userId as any })
      queryParams.walletId = wallet.id as any
    } else {
      queryParams.walletId = walletId as any
    }
  } finally {
    loading.value = false
  }
  await getList()
}
defineExpose({ open })
</script>
<style lang="scss" scoped></style>
